package com.tanhua.mangement.controller;

import cn.hutool.core.date.DateUtil;
import com.tanhua.mangement.service.DashboardService;
import com.tanhua.mangement.vo.DistributionVo;
import com.tanhua.mangement.vo.SummaryVo;
import com.tanhua.mangement.vo.UsersVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

@RestController
@RequestMapping("dashboard")
@Slf4j
public class DashboardController {

    @Autowired
    private DashboardService dashboardService;

    /**
     * 注册用户分布
     * @param sd
     * @param ed
     * @return
     */
    @GetMapping("distribution")
    public ResponseEntity<DistributionVo> distribution(@RequestParam(value = "sd",required = false)Long sd,
                                                       @RequestParam(value = "ed",required = false)Long ed){
        try {
            /*Date startTime = DateUtil.date(sd);  //toDate to String In The SpecifiedFormat
            Date endTime =  DateUtil.date(ed);*/
/*            Date startTime = new Date(Long.parseLong(sd));  //toDate to String In The SpecifiedFormat
            Date endTime = new Date(Long.parseLong(ed));*/
            DistributionVo distributionVo = this.dashboardService.distribution(sd, ed);
            if (distributionVo != null)
                return ResponseEntity.ok(distributionVo);
        } catch (Exception e) {
            log.error("获取数据失败" + e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }



    /**
     * 概要统计信息
     *
     * @return
     */
    @GetMapping("summary")
    public ResponseEntity<SummaryVo> summary() {
        try {
            SummaryVo summaryVo = this.dashboardService.summary();
            if (null != summaryVo) {
                return ResponseEntity.ok(summaryVo);
            }
        } catch (Exception e) {
            log.error("概要统计信息出错"+e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 新增、活跃用户、次日留存率
     *
     * @param sd   开始时间
     * @param ed   结束时间
     * @param type 101 新增 102 活跃用户 103 次日留存率
     * @return
     */
    @GetMapping("users")
    public ResponseEntity<UsersVo> users(@RequestParam("sd") Long sd,
                                         @RequestParam("ed") Long ed,
                                         @RequestParam("type") String type) {
        try {
            UsersVo usersVo = this.dashboardService.users(sd, ed, type);
            if (null != usersVo) {
                return ResponseEntity.ok(usersVo);
            }
        } catch (Exception e) {
            log.error("新增、活跃用户、次日留存率出错,sd="+sd+",ed="+ed+",type="+type,e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();

    }
}
